package com.gitee.feizns.explore.data_structure.linked_list.singly.numComponents;

import com.gitee.feizns.explore.data_structure.linked_list.singly.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author feizns
 * @since 2020/3/6
 */
public class NumComponents {

    public static void main(String[] args) {
        System.out.println(numComponents(create(), new int[]{0, 1, 3}));
    }

    private static ListNode create() {
        ListNode ret = new ListNode(0);
        ret.next = new ListNode(1);
        ret.next.next = new ListNode(2);
        ret.next.next.next = new ListNode(3);
        return ret;
    }

    public static int numComponents(ListNode head, int[] G) {
        Set<Integer> e = new HashSet<>();
        for (int i = 0; i < G.length; i++)
            e.add(G[i]);
        int ret = 0;
        int mark = 0;
        for (ListNode tmp = head; tmp != null; tmp = tmp.next) {
            if ( e.contains(tmp.val) )
                mark = 1;
            else {
                ret += mark;
                mark = 0;
            }
        }
        ret += mark;
        return ret;
    }

}
